package com.sport.config.security;

import com.sport.config.security.contents.SecurityContents;
import com.sport.config.security.filter.JwtAuthenticationFilter;
import com.sport.config.security.handler.JwtAccessDeniedHandler;
import com.sport.config.security.handler.JwtAuthenticationEntryPoint;
import com.sport.config.security.service.userDetailServiceImpl;
import com.sport.entity.SysUser;
import com.sport.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

//权限基本配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    userDetailServiceImpl userDetailService;

    @Autowired
    JwtAccessDeniedHandler accessDeniedHandler;
    @Autowired

    JwtAuthenticationEntryPoint authenticationEntryPoint;

//    @Autowired
//    JwtAuthenticationFilter authenticationFilter;
//自定义登录逻辑的配置，也就配置到securrity中进行认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    }
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

//配置白名单
    @Override
    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().antMatchers("/user/login");
        web.ignoring().antMatchers("/swagger-ui.html");
        web.ignoring().antMatchers(SecurityContents.NONE_SECURITY_URL_PATTERNS);
    }

//核心配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //使用jwt，关闭csrf
        http.csrf().disable();
        //基于token认证，关闭session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //配置白名单  除白名单以外的都进行认证
        http.authorizeRequests().anyRequest().authenticated();
        //禁用缓存
        http.headers().cacheControl();
        //添加jwt登录授权的过滤器
        http.addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class);
        //添加未授权和未登录的返回结果
        http.exceptionHandling().accessDeniedHandler(accessDeniedHandler).authenticationEntryPoint(authenticationEntryPoint);
    }
    @Bean
    public JwtAuthenticationFilter authenticationFilter() {
        return new JwtAuthenticationFilter();
    }
}
